package com.daoimpl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bean.Exam;
import com.dao.IExamDao;
import com.utils.DBUtil;

public class ExamDaoImpl implements IExamDao {

	public int add(Exam exam) {
		String sql = "insert into exam(e_name,e_starttime,e_teacher,e_autostart,e_isend,e_file,e_clear,e_isstart) values (?,?,?,?,?,?,?,?)";
		int result = 0;
		// 为sql语句参数赋值ֵ
		try {
			PreparedStatement ps = DBUtil.executePreparedStatement(sql);
			ps.setString(1, exam.getE_name());//考试名称
			ps.setString(2, exam.getE_starttime());//考试时间
			ps.setString(3, exam.getE_teacher());//创建人
			ps.setBoolean(4, exam.getE_autostart());//是否自动开始
			ps.setBoolean(5, false);//是否结束
			ps.setBoolean(6, false);//是否归档
			ps.setBoolean(7, false);//是否清理
			ps.setBoolean(8, false);//是否正在进行
			result = ps.executeUpdate();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	public int delete(String username) {
		int result = 0;
		String sql = "delete from exam where e_name='" + username + "'";
		try {
			result = DBUtil.executeUpdate(sql);
			DBUtil.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public List<Exam> allexamsearch() {
		List<Exam> list = new ArrayList<Exam>();
		String sql = "SELECT * FROM exam";
		try {
			ResultSet rs = DBUtil.executeQuery(sql);
			while (rs.next()) {
				Exam exam = new Exam();
				exam.setE_name(rs.getString("e_name"));//查询考试名
				exam.setE_starttime(rs.getString("e_starttime"));//开始时间
				exam.setE_teacher(rs.getString("e_teacher"));//创建人
				exam.setE_examination(rs.getString("e_examination"));//考试信息
				exam.setE_isend(rs.getBoolean("e_isend"));//是否结束
				exam.setE_autostart(rs.getBoolean("e_autostart"));//是否自动开始
				exam.setE_file(rs.getBoolean("e_file"));//是否归档
				exam.setE_clear(rs.getBoolean("e_clear"));//是否清理
				exam.setE_isstart(rs.getBoolean("e_isstart"));//是否开始
				list.add(exam);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public int update(Exam exam, String examname) {
		System.out.println(examname);
		System.out.println(exam.getE_name()+" "+exam.getE_starttime()+" "+exam.getE_teacher()+" "+
		" "+exam.getE_examination()+" "+exam.getE_isend()+" "+exam.getE_autostart()+" "+exam.getE_file()+" "+
		exam.getE_clear()+" "+exam.getE_isstart());
		String sql = "update exam set e_name= '" + exam.getE_name() + "',e_starttime= '" + exam.getE_starttime()
				+ "',e_teacher= '" + exam.getE_teacher() + "',e_examination= '" + exam.getE_examination()
				+ "',e_isend= " + exam.getE_isend() + ",e_autostart= " + exam.getE_autostart() + ",e_file= "
				+ exam.getE_file() + ",e_clear= " + exam.getE_clear() + ",e_isstart= " + exam.getE_isstart()
				+ " where e_name='" + examname + "'";
		int result = 0;
		try {
			result = DBUtil.executeUpdate(sql);
			DBUtil.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//System.out.println(result);
		return result;
	}

	public Exam singleexamsearch(String examname) {
		Exam exam = new Exam();
		String sqlSearch = "select * from exam where e_name='" + examname + "'";
		ResultSet rs = null;
		rs = DBUtil.executeQuery(sqlSearch);
		try {
			while (rs.next()) {
				exam.setE_name(rs.getString("e_name"));
				exam.setE_starttime(rs.getString("e_starttime"));
				exam.setE_teacher(rs.getString("e_teacher"));
				exam.setE_examination(rs.getString("e_examination"));
				exam.setE_isend(rs.getBoolean("e_isend"));
				exam.setE_autostart(rs.getBoolean("e_autostart"));
				exam.setE_file(rs.getBoolean("e_file"));
				exam.setE_clear(rs.getBoolean("e_clear"));
				exam.setE_isstart(rs.getBoolean("e_isstart"));
			}
			DBUtil.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return exam;
	}

}
